﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Web.UI;
using Model;
using System.Collections;

namespace Web.admin.BasicManage.students
{
    public partial class AddStudent : System.Web.UI.Page
    {
        BLL.StudentService studentservice = new BLL.StudentService();
        protected static BoStudents studentsorg;
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {
                if (Request.QueryString["OPERATION"] == "Modify")
                {
                    ModifyStudent(Request.QueryString["STU_ID"]);
                }
                else
                {
                    DDL_AcademyBind("未确定");
                    DDL_LocalBind("");
                }

            }
        }

        /// <summary>
        /// 修改绑定方法
        /// </summary>
        /// <param name="stu_id">学生id</param>
        protected void ModifyStudent(string stu_id)
        {
            BLL.StudentService studentService = new BLL.StudentService();
            BLL.AcademyService academyService = new BLL.AcademyService(); 
            BoStudents studentModel = studentService.GetV_Student(stu_id);
            TextBoxName.Text = studentModel.STU_NAME;
            TextBoxCode.Text = studentModel.STU_CODE;
            TextBoxUsm.Text = studentModel.STU_USERNAME;
            TextBoxPwd.Text = studentModel.STU_PASSWORD;
            if(studentModel.STU_GENDER == "1")
                Gender_Male.Checked=true;
            else
                Gender_Femal.Checked = true;

            DDL_AcademyBind("未确定");
            DDL_ClassesBind(null);
            DDL_LocalBind("");

            DDL_Classes.SelectedValue = studentModel.STU_CLASSES;
            DDL_Academy.SelectedValue = studentModel.academy.ACA_ID;
            TextBoxGrade.Text = studentModel.STU_GRADE;
            studentsorg = studentModel;
            ACAIDBOX.Value = stu_id;
        }

        /// <summary>
        /// 对学院的绑定
        /// </summary>
        /// <param name="FirstText">首项</param>
        protected void DDL_AcademyBind(string FirstText)
        {
            DDL_Academy.Items.Clear();
            if (!string.IsNullOrEmpty(FirstText))
            {
                DDL_Academy.Items.Add(new ListItem(FirstText,""));
            }
            BLL.AcademyService AcademyService = new BLL.AcademyService();
            IList<BoAcademy> academies = AcademyService.getAllAcademies();
            foreach (BoAcademy model in academies)
            {
                DDL_Academy.Items.Add(new ListItem(model.ACA_NAME, model.ACA_ID));
            }

        }
        /// <summary>
        /// 对校区的绑定
        /// </summary>
        /// <param name="FirstText">首项</param>
        protected void DDL_LocalBind(string FirstText)
        {
            DDL_Local.Items.Clear();
            if (!string.IsNullOrEmpty(FirstText))
            {
                DDL_Local.Items.Add(new ListItem(FirstText, ""));
            }
            BLL.LocalService localservice = new BLL.LocalService();
            IList<BoLocal> LocalList = localservice.getAllLocal();
            foreach (BoLocal model in LocalList)
            {
                DDL_Local.Items.Add(new ListItem(model.LOC_NAME,model.LOC_ID));
            }
        }

        /// <summary>
        /// 对班级的绑定
        /// </summary>
        /// <param name="academyId">学院id</param>
        protected void DDL_ClassesBind(object academyId)
        {
            BLL.ClassService classesservice = new BLL.ClassService();
            if (academyId != null && !string.IsNullOrEmpty(academyId.ToString()))
            {

                DDL_Classes.DataSource = classesservice.getClassesByAcademy(academyId.ToString());
                DDL_Classes.DataTextField = "CLA_NAME";
                DDL_Classes.DataValueField = "CLA_ID";
                DDL_Classes.DataBind();
            }
            else
            {
                DDL_Classes.DataSource = classesservice.GetAllClasses("");
                DDL_Classes.DataTextField = "CLA_NAME";
                DDL_Classes.DataValueField = "CLA_ID";
                DDL_Classes.DataBind();
            }
        }

        /// <summary>
        /// 学院改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DDL_AcademyChange(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(DDL_Academy.SelectedValue))
            {
                DDL_ClassesBind(DDL_Academy.SelectedValue);
            }
            else
                DDL_Classes.Items.Clear();
        }

        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            //BLL.StudentService studentservice = new BLL.StudentService();
            BoStudents studentmodel = new BoStudents();
            studentmodel.STU_NAME = TextBoxName.Text;
            studentmodel.STU_CODE = TextBoxCode.Text;
            studentmodel.STU_USERNAME = TextBoxUsm.Text;
            studentmodel.STU_PASSWORD = TextBoxPwd.Text;
            studentmodel.STU_GENDER = Gender_Femal.Checked ? "0" : "1";
            studentmodel.STU_CLASSES = DDL_Classes.SelectedValue;
            studentmodel.STU_GRADE = TextBoxGrade.Text;
            //studentservice.CreateStudent(studentmodel);
            //if (string.IsNullOrEmpty(ACAIDBOX.Value))
            //{
            //    studentservice.UpdateStudent(studentmodel);
            //}
            //else
            //{
            //    studentmodel.STU_ID = ACAIDBOX.Value;
            //    studentservice.UpdateStudent(studentmodel);
            //}
            //Response.Redirect("STU_List.aspx");

            BoCheckRepat repeat = new BoCheckRepat();
            repeat.tablename = "Students";
            repeat.constr = "STU_IsShow";

            Hashtable keyValue = new Hashtable();
            keyValue.Add("STU_CODE", studentmodel.STU_CODE);

            repeat.content = keyValue;
            if (string.IsNullOrEmpty(ACAIDBOX.Value))
            {
                if (checkrepeat.checkrepeatCount(repeat) > 0)
                {
                    Response.Write("<script>alert('此对象已经存在，不允许重复')</script>");
                }
                else
                {
                    studentservice.CreateStudent(studentmodel);
                    Response.Redirect("STU_List.aspx");
                }
            }
            else
                {
                studentmodel .STU_ID= ACAIDBOX.Value;
                studentservice.UpdateStudent(studentmodel);
                Response.Redirect("STU_List.aspx");
                }
        }
    }
}